/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.ui;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.BorderFactory;
//import javax.swing.DropMode;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import net.worlds.WorldsException;
import net.worlds.object.GameObject;


public class PartsTree extends JTree{
    private DefaultTreeModel model;
    private PartTreeNode rootNode = null;
    public PartsTree()
    {
        super();
        model = new DefaultTreeModel(null);
        addKeyListener(new KeyListener()
        {

            public void keyTyped(KeyEvent e) {

            }

            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode() == KeyEvent.VK_SHIFT || e.getKeyCode() == KeyEvent.VK_CONTROL)
                    UI.shiftPressed = true;
                else if(e.getKeyCode() == KeyEvent.VK_DELETE)
                    try {
                    UI.deleteSelection();
                } catch (WorldsException ex) {
                    throw new RuntimeException(ex);
                }
            }

            public void keyReleased(KeyEvent e) {
                if(e.getKeyCode() == KeyEvent.VK_SHIFT || e.getKeyCode() == KeyEvent.VK_CONTROL)
                    UI.shiftPressed = false;
            }

        });
        addMouseListener(new TreeMouseAdapter());
        setRootVisible(false);
        setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
        setModel(model);
        PartsTreeSelectionModel selModel = new PartsTreeSelectionModel();
        setSelectionModel(selModel);
        getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
        addTreeSelectionListener(new PartsTreeSelectionListener());
        setTransferHandler(new PartsTreeTransferHandler());
        this.setCellRenderer(new PartsTreeCellRenderer());
        //setDropMode(DropMode.ON);
        setDragEnabled(true);

    }
    @Override
    public DefaultTreeModel getModel()
    {
        return model;
    }
    public void setSelectionPath(final TreePath path, final boolean auto)
    {
        SwingUtilities.invokeLater(new Runnable()
        {

            public void run() {
                ((PartsTreeSelectionModel)getSelectionModel()).setSelectionPath(path, auto);
            }
        });
    }
    public void setSelectionPaths(final TreePath[] paths, final boolean auto)
    {
        SwingUtilities.invokeLater(new Runnable()
        {

            public void run() {
        ((PartsTreeSelectionModel)getSelectionModel()).setSelectionPaths(paths, auto);
            }
        });
    }

    public void create(final GameObject root)
    {
        rootNode = root.getTreeNode();
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                model.setRoot(rootNode);
                addObject(root);
            }
        });
    }
    private void addObject(GameObject obj)
    {
        try {
            if(obj.getParent() != null)
                UI.onChildAdded(obj.getParent(), obj);
            expandPath(new TreePath(obj.getTreeNode().getPath()));
            synchronized(obj.getChildren())
            {
                for(GameObject c : obj.getChildren())
                {
                    addObject(c);
                }
            }
        } catch (WorldsException ex) {
            throw new RuntimeException(ex);
        }
    }
}
